﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	UserValue.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Diagnostics;

namespace Smart.Common.Entities
{
	/// <summary>
	/// User or Group value
	/// </summary>
	[Serializable]
	[DebuggerStepThrough]
	public class UserValue
	{
		/// <summary>
		/// User or Group ID
		/// </summary>
		public Identifier ID { get; set; }

		/// <summary>
		/// User or Group Title
		/// </summary>
		public string Title { get; set; }

		/// <summary>
		/// User or Group value
		/// </summary>
		public UserValue(Identifier id)
		{
			ID = id.IsEmpty ? Identifier.Empty : id;
			Title = null;
		}

		/// <summary>
		/// User or Group value
		/// </summary>
		public UserValue(Identifier id, string title)
		{
			if (id.IsEmpty)
			{
				ID = Identifier.Empty;
				Title = null;
			}
			else
			{
				ID = id;
				Title = title;
			}
		}

		/// <summary>
		/// Checks whether this value is empty
		/// </summary>
		public bool IsEmpty
		{
			get { return ID.IsEmpty; }
		}

		/// <summary>
		/// Set new user or group value
		/// </summary>
		public void Set(Identifier id)
		{
			ID = id.IsEmpty ? Identifier.Empty : id;
			Title = null;
		}

		/// <summary>
		/// Set new user or group value
		/// </summary>
		public void Set(Identifier id, string title)
		{
			if (id.IsEmpty)
			{
				ID = Identifier.Empty;
				Title = null;
			}
			else
			{
				ID = id;
				Title = title;
			}
		}

		/// <summary>
		/// Format in "id;#title"
		/// </summary>
		public override string ToString()
		{
			return (IsEmpty ? null : SPH.FormatLookupValue(ID.Int, Title));
		}

		/// <summary>
		/// Compare user or group values only by ID property
		/// </summary>
		public bool Equals(UserValue obj)
		{
			return ID.Equals(obj.ID);
		}

		/// <summary>
		/// Compare user or group values only by ID property
		/// </summary>
		public override bool Equals(object obj)
		{
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			return
				Equals((UserValue)obj);
		}

		/// <summary>
		/// Hash code using ID property
		/// </summary>
		public override int GetHashCode()
		{
			return ID.GetHashCode();
		}

		public static bool operator ==(UserValue uv1, UserValue uv2)
		{
			if ((object)uv1 == null && (object)uv2 == null)
				return true;

			if ((object)uv1 == null || (object)uv2 == null)
				return false;

			return
				uv1.Equals(uv2);
		}

		public static bool operator !=(UserValue uv1, UserValue uv2)
		{
			if ((object)uv1 == null && (object)uv2 == null)
				return false;

			if ((object)uv1 == null || (object)uv2 == null)
				return true;

			return
				!uv1.Equals(uv2);
		}

		public static bool operator ==(UserValue uv1, int uv2)
		{
			if ((object)uv1 == null)
				return false;

			return
				uv1.ID.Int == uv2;
		}

		public static bool operator !=(UserValue uv1, int uv2)
		{
			if ((object)uv1 == null)
				return true;

			return
				uv1.ID.Int != uv2;
		}
	}
}